/*
 * 
 */
package at.hcmut.dev.mobile.barcamp;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;


/**
 * The Class MyLocationService provide a service which can be used by any activity to update current location (geo coding location).
 * <p>The caller activity should call this service by startActivityForResult() method since {@link MyLocationService} will push result data
 * by involking {@link onActivityResult} method.
 * <p>
 * In the case of updating location successfully, the resultCode parameter will be set as RESULT_OK.
 */
public class MyLocationService extends Activity {
	ProgressDialog progressDialog ;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.out.println("MyLocationService, oncreate,");
		progressDialog = ProgressDialog.show(this, "", "Retrieving your location");
		progressDialog.setCancelable(true);
		progressDialog.setOnCancelListener(new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				LocationService.Dismiss();
			}
		});
		getLocation();

	}

	private void exitWithLocation(Location location) {
		CommonUtilities.location = location;
		if (getIntent().getExtras() != null)
			setResult(RESULT_OK, (new Intent()).setAction("inline-data").putExtras(getIntent().getExtras()));
		else
			setResult(RESULT_OK);
		progressDialog.dismiss();
		finish();
	}

	private void exitWithoutLocation() {
		CommonUtilities.location = null;
		if (getIntent().getExtras() != null)
			setResult(RESULT_CANCELED, (new Intent()).setAction("inline-data").putExtras(getIntent().getExtras()));
		else
			setResult(RESULT_CANCELED);
		progressDialog.dismiss();
		finish();
	}

	private void getLocation() {
		LocationService.Register(this, new LocationResult() {

			@Override
			public void OnAction(Location location) {
				exitWithLocation(location);
			}

			@Override
			public void OnFailed(String message) {
					if(message == "")
					{
						exitWithoutLocation();
						return;
					}
//					Messenger.showMessageBox(MyLocationService.this, message, new MessageBoxResult() {
//						@Override
//						public void onResult() {
//							exitWithoutLocation();
//						}
//					});
			}

			@Override
			public void OnUpdate(Location location) {
			}
		});
		new LocationTimer().execute(60);// set timer within 60 seconds.
	}

	/**
	 * This class implement background timer which count the time to request
	 * location. After a specific period of time, this timer will cancel
	 * requesting then exit the application.
	 * 
	 * @author IHNEL
	 * 
	 */
	public class LocationTimer extends AsyncTask<Integer, Integer, Integer> {

		@Override
		protected Integer doInBackground(Integer... params) {
			try {
				Thread.sleep(params[0] * 1000);
			} catch (InterruptedException e) {
			}
			return 0;
		}

		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			// does nothing if requesting successful.
			if (CommonUtilities.location != null)
				return;
			// Exits application if requesting take no result after a specific
			// period of time.
			LocationService.Dismiss();
			exitWithoutLocation();
		}
	}

}
